import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AjaxServlet
 */
public class AjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Map<String, List<String>> data;

	/**
	 * Default constructor.
	 */
	public AjaxServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String rp =  new String(request.getParameter("rp").getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(rp);
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		List<String> list = data.get(rp);
		if (list != null) {
			String jsonList = JSONUtil.objetc2Json(list);
			System.out.println(jsonList);
			writer.println(jsonList);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String rp = request.getParameter("rp");
		String rr = request.getParameter("rr");
		System.out.println(rp);
		System.out.println(rr);
		
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		String city1 = "珠海市";
		String area1 = "香洲区";
		String area2 = "金湾区";
		String area3 = "斗门区";

		String city2 = "哈尔滨市";
		String area4 = "道里区";
		String area5 = "道外区";
		String area6 = "南岗区";

		List<String> list1 = new ArrayList<String>();
		list1.add(area1);
		list1.add(area2);
		list1.add(area3);

		List<String> list2 = new ArrayList<String>();
		list2.add(area4);
		list2.add(area5);
		list2.add(area6);

		data = new HashMap<String, List<String>>();
		data.put(city1, list1);
		data.put(city2, list2);
	}

}
